package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.BitmapTypeRequest;
import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.ReserveChildVH;
import com.centaline.androidsalesblog.adapter.viewholder.ReserveParentVH;
import com.centaline.androidsalesblog.iml.ReserveItemCallback;
import com.centaline.androidsalesblog.model.businissobject.ReserveStaffNoBo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.model.databaseobject.StaffDo;
import com.centaline.androidsalesblog.utils.ContactUtil;
import com.centaline.androidsalesblog.utils.DateUtil;
import com.centaline.androidsalesblog.utils.PriceUtil;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.glide.RoundImageViewTarget;
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractExpandableItemAdapter;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 已约看
 * Created by Ruanzb on 16/5/9.
 */
public class ReservationAdapter extends AbstractExpandableItemAdapter<ReserveParentVH, ReserveChildVH> {

    private BitmapTypeRequest<String> mBitmapTypeRequest;
    private DrawableRequestBuilder<String> requestBuilder;
    private ArrayList<ReserveStaffNoBo> reserveStaffNoBoList;
    private ReserveItemCallback reserveItemCallback;
    private LayoutInflater inflater;
    private Context context;
    private boolean imEnable;

    public ReservationAdapter(Context context, BitmapTypeRequest<String> mBitmapTypeRequest
            , DrawableRequestBuilder<String> requestBuilder, ArrayList<ReserveStaffNoBo> reserveStaffNoBoList, ReserveItemCallback reserveItemCallback) {
        this.context = context;
        this.mBitmapTypeRequest = mBitmapTypeRequest;
        this.requestBuilder = requestBuilder;
        this.reserveStaffNoBoList = reserveStaffNoBoList;
        this.reserveItemCallback = reserveItemCallback;
        inflater = LayoutInflater.from(context);
        setHasStableIds(true);
    }

    public void setImEnable(boolean imEnable) {
        this.imEnable = imEnable;
    }

    @Override
    public int getGroupCount() {
        return reserveStaffNoBoList.size();
    }

    @Override
    public int getChildCount(int groupPosition) {
        return reserveStaffNoBoList.get(groupPosition).getPostList().size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return reserveStaffNoBoList.get(groupPosition).getUpdateTime();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return reserveStaffNoBoList.get(groupPosition).getPostList().get(childPosition).getPostId().hashCode();
    }

    @Override
    public ReserveParentVH onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        return new ReserveParentVH(inflater.inflate(R.layout.item_reserve_parent, parent, false));
    }

    @Override
    public ReserveChildVH onCreateChildViewHolder(ViewGroup parent, int viewType) {
        return new ReserveChildVH(inflater.inflate(R.layout.item_post_list, parent, false));
    }

    @Override
    public void onBindGroupViewHolder(ReserveParentVH holder, final int groupPosition, int viewType) {
        final StaffDo staffDo = reserveStaffNoBoList.get(groupPosition).getStaffDo();
        if (staffDo == null) {
//            holder.img_staff.setImageResource(R.drawable.ic_staff_default);
        } else {
            if (GlideLoad.needLoad(true)) {
                mBitmapTypeRequest
                        .load(staffDo.getImgUrl())
                        .placeholder(R.drawable.ic_staff_default)
                        .error(R.drawable.ic_staff_default)
                        .centerCrop()
                        .into(new RoundImageViewTarget(holder.img_staff));
            } else {
                mBitmapTypeRequest
                        .load("")
                        .placeholder(R.drawable.ic_staff_default)
                        .error(R.drawable.ic_staff_default)
                        .centerCrop()
                        .into(new RoundImageViewTarget(holder.img_staff));
            }

            holder.atv_name.setText(staffDo.getCnName());

            holder.atv_msg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ContactUtil.sendMsg(context, staffDo.getMobile());
                }
            });
            holder.atv_phone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(context)
                            .setMessage(String.format(Locale.CHINA, "拨打:%s,%s",
                                    staffDo.getBigCode(), staffDo.getExtCode()))
                            .setPositiveButton("拨打", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ContactUtil.dial(context, staffDo.getBigCode(), staffDo.getExtCode());

                                }
                            }).setNegativeButton("取消", null)
                            .create()
                            .show();
                }
            });
            holder.atv_im.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reserveItemCallback.staffCallback(staffDo, 1);
                }
            });
        }
        holder.atv_im.setVisibility(imEnable ? View.VISIBLE : View.GONE);
        String time = DateUtil.format(reserveStaffNoBoList.get(groupPosition).getUpdateTime() * 1000, DateUtil.FORMAT2);
        holder.atv_entrust_time.setText(String.format(Locale.CHINA, "委托时间: %s", time));
//        holder.atv_name.setText(staffDo == null ? "" : staffDo.getCnName());
        int size = reserveStaffNoBoList.get(groupPosition).getPostList().size();
        holder.atv_entrust_fy.setText(String.format(Locale.CHINA, "委托房源 (%d)", size));

    }

    @Override
    public void onBindChildViewHolder(ReserveChildVH holder, final int groupPosition, final int childPosition, int viewType) {

        final PostDo postDo = reserveStaffNoBoList.get(groupPosition).getPostList().get(childPosition);

        if ("s".equalsIgnoreCase(postDo.getPostType())) {//二手房
            GlideLoad.load(new GlideLoad.Builder(requestBuilder, postDo.getThumbPath())
                    .placeHolder(R.drawable.ic_centa_square_logo)
                    .error(R.drawable.ic_centa_square_logo)
                    .openWifiSwitch()
                    .into(holder.img_logo));
            holder.atv_title.setText(postDo.getTitle());
            holder.atv_tag_fir.setText(String.format(Locale.CHINA, "%d室%d厅  %.2f平  %s",
                    postDo.getBedroomCount(),
                    postDo.getSittingRoomCount(),
                    postDo.getSize(),
                    TextUtils.isEmpty(postDo.getDirection()) ? "" : postDo.getDirection()));
            holder.atv_tag_thi.setVisibility(View.GONE);
            holder.atv_tag_sec.setText(postDo.getCnEstate());
            holder.atv_price.setText(PriceUtil.salePrice(postDo.getPrice()));
            holder.atv_distance.setVisibility(View.GONE);
            holder.rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reserveItemCallback.postItemCallbak(postDo);
                }
            });
            holder.customListTagView.setTags(postDo.getKeyWords());
            holder.customListTagView.setVisibility(View.VISIBLE);
        } else {//租房
            GlideLoad.load(new GlideLoad.Builder(requestBuilder, postDo.getThumbPath())
                    .placeHolder(R.drawable.ic_centa_square_logo)
                    .error(R.drawable.ic_centa_square_logo)
                    .openWifiSwitch()
                    .into(holder.img_logo));
            holder.atv_title.setText(postDo.getTitle());
            holder.atv_tag_fir.setText(String.format(Locale.CHINA, "%d室%d厅  %s",
                    postDo.getBedroomCount(),
                    postDo.getSittingRoomCount(),
                    TextUtils.isEmpty(postDo.getDirection()) ? "" : postDo.getDirection()));
            holder.atv_tag_sec.setText(postDo.getFitMent());
            holder.atv_tag_thi.setText(String.format(Locale.CHINA, "%s  %s", postDo.getGscope(), postDo.getCnEstate()));
            holder.atv_tag_thi.setVisibility(View.VISIBLE);
            holder.atv_distance.setVisibility(View.GONE);
            holder.atv_price.setText(PriceUtil.rentPrice(postDo.getRental()));
            holder.rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reserveItemCallback.postItemCallbak(postDo);
                }
            });
            holder.customListTagView.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onCheckCanExpandOrCollapseGroup(ReserveParentVH holder, int groupPosition, int x, int y, boolean expand) {
        return expand;
    }

}
